﻿using UnityEngine;
using System;
using UnityEngine.UI;
using System.Collections;
using System.Xml;
using UnityEngine.UI;
using System.Collections.Generic;

public class buildingInformation : MonoBehaviour, ICardboardGazeResponder
{
    /// <summary>
    /// This script should be set for each building.
    /// When the gaze click the building, the event can be controled.
    /// </summary>
    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void SetGazedAt(bool gazedAt)
    {

    }

    public void OnGazeEnter()
    {
        SetGazedAt(true);
    }

    public void OnGazeExit()
    {
        SetGazedAt(false);
    }

    public void OnGazeTrigger()
    {
        GameObject.Find("messageButton").GetComponent<RectTransform>().localPosition = new Vector3(0, 436, -64);
        GameObject.Find("messageButton").GetComponentInChildren<Text>().text = GetTextInfomationFromService(Convert.ToInt32(this.transform.name));
    }

    string GetTextInfomationFromService(int id)
    {
        string baseUrl = "http://112.126.69.154:8090/iserver/services/data-WHUvr360/rest/data/datasources/name/VR360/datasets/name/Site/features/{0}.xml";
        string url = string.Format(baseUrl, id);
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(url);

        Dictionary<string, string> result = new Dictionary<string, string>();

        XmlNode valueNodes = xmlDoc.SelectSingleNode("Feature/fieldValues");

        XmlNodeList valueChilds = valueNodes.ChildNodes;
        XmlNode nameNodes = xmlDoc.SelectSingleNode("Feature/fieldNames");

        XmlNodeList nameChilds = nameNodes.ChildNodes;

        if (valueChilds.Count != nameChilds.Count)
        {
            throw new XmlException("格式错误！");
        }

        for (int i = 0; i < valueChilds.Count; i++)
        {
            if (nameChilds[i].FirstChild.Value == "SITEPROPERTY"
                || nameChilds[i].FirstChild.Value == "SITENAME")
            {
                result.Add(nameChilds[i].FirstChild.Value, valueChilds[i].FirstChild.Value);
            }
        }
        return result["SITEPROPERTY"];
    }
}
